Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more Spark Expressions #1724

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

lucas-nelson-uiuc
Copy link
Contributor

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

Added the following methods to SparkLikeExpr and SparkLikeNamespace:

  • any
  • all
  • null_count
  • any_horizontal

Copied respective tests over - couldn't run them without Java on my machine but running them locally on their respective test datasets worked for me.

Let me know if anything needs to be updated!

@lucas-nelson-uiuc lucas-nelson-uiuc changed the title Missing spark expr feat: add more Spark Expressions Jan 4, 2025
Copy link
Collaborator

@EdAbati EdAbati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for looking into this! 🙏

tests/spark_like_test.py Outdated Show resolved Hide resolved
narwhals/_spark_like/expr.py Show resolved Hide resolved
@lucas-nelson-uiuc
Copy link
Contributor Author

Hey @EdAbati ,

Took an initial swing at implementing the replace_strict() method - think I took care of everything except for handling the test_replace_non_full test (checks that replacement is exhaustive). Left some thoughts and other questions in my commit - lmk what you think!

Copy link
Collaborator

@EdAbati EdAbati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating! :)

I left another couple of small comments

IMO we can just merge all, any and null_count here and worry about the rest in a follow up

Comment on lines +212 to +217
def _all(_input: Column) -> Column:
from pyspark.sql import functions as F # noqa: N812

return F.bool_and(_input)

return self._from_call(_all, "all", returns_scalar=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We simplified a bit the other methods, we can refactor as

Suggested change
def _all(_input: Column) -> Column:
from pyspark.sql import functions as F # noqa: N812
return F.bool_and(_input)
return self._from_call(_all, "all", returns_scalar=True)
from pyspark.sql import functions as F # noqa: N812
return self._from_call(F.bool_and, "all", returns_scalar=True)

Comment on lines +220 to +225
def _any(_input: Column) -> Column:
from pyspark.sql import functions as F # noqa: N812

return F.bool_or(_input)

return self._from_call(_any, "any", returns_scalar=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _any(_input: Column) -> Column:
from pyspark.sql import functions as F # noqa: N812
return F.bool_or(_input)
return self._from_call(_any, "any", returns_scalar=True)
from pyspark.sql import functions as F # noqa: N812
return self._from_call(F.bool_or, "any", returns_scalar=True)

same as above


return self._from_call(_null_count, "null_count", returns_scalar=True)

def replace_strict(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tempted to say that this should not be implemented for now and just raise a NotImplementedError. (as we do in Dask)
We would need to be able to access the dataframe (and collect the results) to get the distinct values of the column.

@FBruzzesi and @MarcoGorelli any thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am tempted to say that this should not be implemented for now and just raise a NotImplementedError. (as we do in Dask)

Sure we can evaluate if and how to support replace_strict later on. Super excited to ship the rest for now 🙌🏼

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants